Skip to content

[opentitanlib] further refactors to UartConsole#28805

Merged
nbdd0121 merged 6 commits intolowRISC:masterfrom
nbdd0121:uart-console
Nov 24, 2025
Merged

[opentitanlib] further refactors to UartConsole#28805
nbdd0121 merged 6 commits intolowRISC:masterfrom
nbdd0121:uart-console

Conversation

@nbdd0121
Copy link
Contributor

This adds an utility for UART (consoles) Broadcaster which can be used to disseminate received data to multiple readers and each readers be able to read full history.

This can be useful when there're multiple users of UART that need to handle inputs simultaneously. The opentitantool's logfile option has been refactored to do this so it no longer needs to be part of UartConsole.

Previously `set_break` needs to be on `ConsoleDevice` because `UartConsole`
expects it to be there, so it can turn stdin breaks into console breaks.
This feature is now moved to `opentitantool console` (which was the only place
this feature was used), so the `set_break` doesn't need to be on `ConsoleDevice`
anymore (and also this capability is only available on `Uart` impls anyway).

Signed-off-by: Gary Guo <gary.guo@lowrisc.org>
@nbdd0121 nbdd0121 requested a review from jwnrt November 21, 2025 17:06
@nbdd0121 nbdd0121 requested a review from a team as a code owner November 21, 2025 17:06
Copy link
Contributor

@jwnrt jwnrt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks okay, thanks

Out of interest did you look for existing libraries to do the broadcast? Maybe something like the crossbeam mpmc?

let mut inner = self.inner.lock().unwrap();

let pos = inner.reader_pos[self.index];
let index = if let Some(index) = inner.reader_pos.iter().position(|x| x.is_none()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: This is slightly hard to follow as a one-liner, could you split the None index finder to a variable?


// Runs an interactive console until CTRL_C is received.
pub fn interact<T>(&mut self, device: &T, stdout: Option<&mut dyn Write>) -> Result<ExitStatus>
pub fn interact<T>(&mut self, device: &T, quiet: bool) -> Result<ExitStatus>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I would personally prefer an API with an enum since it's not obvious what the Boolean does from the call site

#[derive(Clone, Copy, Debug)]
enum Echo {
    Off,
    On,
}

pub fn interact<T>(&mut self, device: &T, echo: Echo) -> Result<ExitStatus>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I plan to remove quiet (probably turn it into a field or setter method) given most users set it to false. Going to leave this to a future PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine with me

@nbdd0121
Copy link
Contributor Author

Out of interest did you look for existing libraries to do the broadcast? Maybe something like the crossbeam mpmc?

There's broadcast in tokio, but it is generic and so you can only do one-element-at-a-time, where I want to just handle bytes.

This can allow multiple concurrent readers to all receive all data.

Signed-off-by: Gary Guo <gary.guo@lowrisc.org>
Signed-off-by: Gary Guo <gary.guo@lowrisc.org>
Reduce nesting level by 1.

Signed-off-by: Gary Guo <gary.guo@lowrisc.org>
Signed-off-by: Gary Guo <gary.guo@lowrisc.org>
We always use `stdout` if it's not `None`, so just use a boolean quiet flag
instead.

Signed-off-by: Gary Guo <gary.guo@lowrisc.org>
@nbdd0121 nbdd0121 added this pull request to the merge queue Nov 24, 2025
Merged via the queue into lowRISC:master with commit 7f0e31e Nov 24, 2025
43 of 47 checks passed
@nbdd0121 nbdd0121 deleted the uart-console branch November 24, 2025 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants